﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace ICSharpCode.WinFormsUI.Controls
{
    public class NToolStripPaintUtils
    {
        public static void DrawItem(Graphics g, ToolStripItem item)
        {
            if (!item.Visible)
                return;

            Rectangle bound = item.Bounds;
            int pos = bound.X;
            int iconSize = 16;

            if (item is ToolStripSeparator)
            {
                pos = pos + 2 * iconSize + 8;
                g.DrawLine(new Pen(SystemBrushes.ControlDark), new Point(pos, bound.Y + bound.Height / 2), new Point(item.Width, bound.Y + bound.Height / 2));
            }
            else
            {
                Brush textBru = new SolidBrush(NToolStripTheme.TextColor);

                if (item.Pressed)
                {
                    textBru = new SolidBrush(NToolStripTheme.HighlightForeColor);
                    g.FillRectangle(new SolidBrush(NToolStripTheme.PressedColor), bound);
                }
                else
                {
                    if (item.Selected)
                    {
                        textBru = new SolidBrush(NToolStripTheme.HighlightForeColor);
                        g.FillRectangle(new SolidBrush(NToolStripTheme.HighlightBackColor), bound);
                    }
                }

                if (item.Image != null)
                {
                    Color foreColor = Color.White;
                    switch (NToolStripTheme.Name)
                    {
                        case "Default":
                            foreColor = Color.Black;
                            break;
                        case "Black":
                            foreColor = Color.White;
                            break;
                    }
                    g.DrawImage(CommonHelper.CreateCoverColorImage(item.Image, foreColor), new Rectangle(pos + 6, bound.Y + (item.Height - iconSize) / 2 + 1, iconSize, iconSize),
                            new Rectangle(0, 0, item.Image.Width, item.Image.Height), GraphicsUnit.Pixel);
                }

                bool hasDrapdownList = false;

                if (item is NToolStripMenuItem)
                {
                    NToolStripMenuItem toolStripItem = (NToolStripMenuItem)item;

                    if (toolStripItem.HasDropDown)
                        hasDrapdownList = true;

                    if (toolStripItem.Checked)
                    {                        
                        Image iconImage = ICSharpCode.WinFormsUI.Controls.Properties.Resources.checked24;
                        g.DrawImage(CommonHelper.CreateCoverColorImage(iconImage, item.Owner.BackColor), new Rectangle(pos + 6, bound.Y + (item.Height - iconSize) / 2 + 1, iconSize, iconSize),
                                new Rectangle(0, 0, iconImage.Width, iconImage.Height), GraphicsUnit.Pixel);
                    }
                }

                pos = pos + 2 * iconSize + 8;

                if (!string.IsNullOrEmpty(item.Text) && (item.DisplayStyle == ToolStripItemDisplayStyle.Text || item.DisplayStyle == ToolStripItemDisplayStyle.ImageAndText))
                {
                    string itemText = item.Text;
                    Font itemFont = new Font(item.Font, FontStyle.Regular);
                    if (item.Text.StartsWith("&"))
                    {
                        itemText = item.Text.Substring(1);
                        itemFont = new Font(item.Font, FontStyle.Regular);
                    }

                    if (item.DisplayStyle == ToolStripItemDisplayStyle.Text || item.Image == null)
                    {
                        g.DrawString(itemText, itemFont, textBru, new Rectangle(pos + 2, bound.Y, item.Width - 4, item.Height), new StringFormat() { LineAlignment = StringAlignment.Center });
                    }
                    else
                    {
                        g.DrawString(itemText, itemFont, textBru, new Rectangle(pos, bound.Y, item.Width - iconSize - 4, item.Height), new StringFormat() { LineAlignment = StringAlignment.Center });
                    }

                }

                if (hasDrapdownList)
                {
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    Point[] pntArr = {
                        new Point(bound.Right-12,bound.Y +(bound.Height -12)/2),
                        new Point(bound.Right-7,bound.Y +(bound.Height -12)/2+5),
                        new Point(bound.Right-12,bound.Y +(bound.Height -12)/2+10)
                    };
                    g.FillPolygon(textBru, pntArr);
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
                }

            }
        }
    }
}
